package com.atguigu.cloud.controller;

import com.atguigu.cloud.service.PaymentFeignService;
import com.atguigu.cloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author : zjj
 * @date : 2021/1/21 17:14
 */

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @Resource
    private PaymentService paymentService;

   /* @Value("${server.port}")
    private String serverPort;*/

    @GetMapping("/order/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentFeignService.paymentInfo_OK(id);
        log.info("*******result:"+result);
        return result;
    }

    @HystrixCommand(fallbackMethod = "paymentInfoFallback" ,commandProperties = {
            // 超时时长设置
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
    })
    @GetMapping("/order/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        String result = paymentFeignService.paymentInfo_TimeOut(id);
        log.info("*******result:"+result);
        return result;
    }


    /**
     * 兜底方法
     * @param id    id
     * @return      返回兜底结果
     */
    public String paymentInfoFallback(@PathVariable("id")Integer id){
        return "我是兜底方法，paymentInfoFallback出错了。";
    }

    //===服务熔断
    @GetMapping("/order/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String result = paymentService.paymentCircuitBreaker(id);
        log.info("*******result:"+result);
        return result;
    }





}
